﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ticTacToe.Domain
{
    public class ComputerPlayer : Player
    {
        public int difficulty { get; set; }     //determines "how smart" the computer plays
        
               
        public ComputerPlayer(char mark, int diff)
        {
            this.username = "ComputerPlayer";
            this.mark = mark;
            this.difficulty = diff;
        }
        
        //given a Gameboard, returns which square on the board to check (1 to 9)
        public int play(Gameboard gb) {
            if (this.difficulty == 1) {
                return randomPlay(gb);
            }
            else if (this.difficulty == 2)
            {
                if (randomGenerator(1, 2) == 1)
                {
                    return randomPlay(gb);
                }
                else {
                    return intelliPlay(gb);
                }
            }
            else
            {
                return intelliPlay(gb);
            }
        }

        private int randomPlay(Gameboard gb)
        {   
            int rand;
            char[] board = gb.getBoard();
            do{
                rand = this.randomGenerator(0,9);
            }while(board[rand] != 'E');
            return rand;
        }

        /**
         * Examines the given gameboard and makes an intelligent play in
         * accordance with the boards state.
         */
        private int intelliPlay(Gameboard gb)
        {
            //TODO: Implement
            throw new NotImplementedException();
        }

        /**
         * Used for generating random numbers for multiple puposes
         */
        private int randomGenerator(int minValue, int maxValue )
        {
            Random r = new Random();
            return r.Next(minValue, maxValue);
        }
    }
}